package com.jinxiangyang.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.Student;
import com.jinxiangyang.service.StudentService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/student")
@ApiModel("学生控制器")
public class StudentController {



    @Autowired
    private StudentService studentService;


    @ApiOperation("获取学生信息")
    @GetMapping("{pageNumber}/{pageSize}")
    @OperateLog(title = "学生模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.OTHER)
    public AjaxResult getPageStudentLike(@PathVariable("pageNumber")  Integer pageNumber,@PathVariable("pageSize") Integer pageSize, Student student){
        IPage<Student> studentByPageAndLike = studentService.getStudentByPageAndLike(pageNumber, pageSize, student);
        if(pageNumber>studentByPageAndLike.getPages()){
            studentByPageAndLike = studentService.getStudentByPageAndLike((int) studentByPageAndLike.getPages(), pageSize, student);
        }
        return AjaxResult.success(studentByPageAndLike);
    }



    @ApiOperation("更新学生信息")
    @PutMapping
    @OperateLog(title = "学生模块",operatorType = OperatorType.STUDENT, businessType = BusinessType.INSERT)
    public AjaxResult updateStudent(@RequestBody Student student){
        return studentService.updateById(student)?AjaxResult.success():AjaxResult.error("修改失败");
    }


    @ApiOperation("添加学生信息")
    @PostMapping
    @OperateLog(title = "学生模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.INSERT)
    public AjaxResult insertStudent(@RequestBody Student student){
        return studentService.save(student)?AjaxResult.success():AjaxResult.error("插入失败");
    }

    @ApiOperation("删除学生信息")
    @DeleteMapping("{id}")
    @OperateLog(title = "学生模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.DELETE)
    public AjaxResult deleteStudentById(@PathVariable("id") Integer id){
        return studentService.removeById(id)?AjaxResult.success():AjaxResult.error("删除失败");
    }

}

